package com.ita.tcp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Attack implements Runnable{
	
	private String host;
	private int port;
	
	public Attack(String host, int port) {
		super();
		this.host = host;
		this.port = port;
	}	
	
	public void attack() throws UnknownHostException, IOException {
		Socket socket = new Socket(host, port);
		BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

		bufferedWriter.write("GET / HTTP/1.1\r\n");
		bufferedWriter.write("Host: www.sohu.com\r\n");
		bufferedWriter.write("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n");
		bufferedWriter.write("\r\n");
		bufferedWriter.flush();

		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			System.out.println(line);
			if (line.contains("</html>")) {
				break;
			}
		}

		bufferedReader.close();
		bufferedWriter.close();
		socket.close();
	}
	

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			attack();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
